#include "../source/common/dispatcher.hpp"
#include "../source/server/rpc_router.hpp"
//需要把服务信息管理集合到服务端中
using namespace zyb_rpc;
using namespace std;
// using ConnectionCallback = std::function<void(const BaseConnection::ptr &)>;
// using CloseCallback = std::function<void(const BaseConnection::ptr &)>;
// using MessageCallback = std::function<void(const BaseConnection::ptr &, BaseMessage::ptr &)>;
void Add(const Json::Value &req, Json::Value &rsp)
{
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp = num1 + num2;
}
int main()
// router的回调设置给dispatcher dispatcher的回调设置给server
{
    auto router = std::make_shared<server::RpcRouter>(); // 服务注册接口，提供给dispatcher的rpc请求处理回调函数
    std::shared_ptr<server::SDescribeFactory> desc_factory(new server::SDescribeFactory());
    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1", server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2", server::VType::INTEGRAL);
    desc_factory->setReturnType(server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    router->registerMethod(desc_factory->build());

    // 上面是用户的操作
    auto cb = std::bind(&server::RpcRouter::onRpcRequest, router.get(), std::placeholders::_1, std::placeholders::_2);

    Dispatcher::ptr dispathcher = make_shared<Dispatcher>(); // Dispatcher分发处理不同类型消息的请求
    dispathcher->registerHandler<RpcRequest>(MType::REQ_RPC, cb);

    BaseServer::ptr server = ServerFactory::create(9090);

    MessageCallback message_cb = std::bind(&Dispatcher::onMessage, dispathcher.get(),
                                           placeholders::_1, placeholders::_2);
    server->setMessageCallback(message_cb);

    server->start();
    //  也就是说 server收到消息后，通过dispatcher模块的onMessage接口进行处理
    //  onMessage接口可以处理不同类型的消息并转发给不同消息的处理模块
    //  如果发现是rpc请求 就调用rpcrouter中寻找 如果注册了的函数 就调用。这些函数都在RpcRouter::onRpcRequest中存储
    //
}
